import  {createSlice} from '@reduxjs/toolkit'
import axios from "axios";

const billStore = createSlice({
    name: 'billStore',
    initialState: {
        billList: []
    },
    reducers:{
        setBillList(state, action){
            state.billList = action.payload
        },
        addBill(state, action){
            state.billList.push(action.payload)
        }
    }
})

const {setBillList, addBill} = billStore.actions
const reducer = billStore.reducer
export default reducer

export const getBillList = () => {
    return async (dispatch) =>{
        const {data}= await axios.get('http://localhost:8888/ka')
        dispatch(setBillList(data))
    }
}

export const addBillList = (bill) => {
    return async (dispatch) =>{
        const res = await axios.post('http://localhost:8888/ka', bill)
        dispatch(addBill(res.data))
    }
}